package com.study.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.study.yygh.order.client.DictFeignClient;
import com.study.yygh.hosp.repository.HospitalRepository;
import com.study.yygh.hosp.service.HospitalService;
import com.study.yygh.model.hosp.Hospital;
import com.study.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tao
 * @create 2021-09-22 14:38
 * @Description
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    // 注入远程调用
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        // 把参数map集合转换为对象 Hospital
        String mapString = JSONObject.toJSONString(paramMap);// map转换为字符串
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        // 判断数据库是否存在相同的数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
        // 如果存在，进行修改
        if (hospitalExist != null) {
            hospitalExist.setStatus(hospital.getStatus());
            hospitalExist.setCreateTime(hospital.getCreateTime());
            hospitalExist.setUpdateTime(new Date());
            hospitalExist.setIsDeleted(0);
            hospitalRepository.save(hospitalExist);
        } else {// 如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    // 医院列表（条件查询分页）
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        // 创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        // hospitalSetQueryVo 转换 Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        // 创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        // 调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example,pageable);

        // 远程调用cmn服务 因为医院设置表中只有医院的基本信息 没有医院等级信息，所以要调用数据字典的微服务
        // 获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item->{
            // item就是每次遍历的Hospital对象
            this.setHospitalHosType(item);
        });
        return pages;
    }

    // 更新医院状态
    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue()==0 || status.intValue()==1){
            // 根据id查询医院信息
            Hospital hospital = hospitalRepository.findById(id).get();
            // 设置修改项目的值
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    // 医院详情信息
    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String,Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        // 医院基本信息 包含等级
        result.put("hospital",hospital);
        // 单独处理更直观
        result.put("bookingRule",hospital.getBookingRule());
        // 不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    // 根据医院编号获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospital!=null){
            return hospital.getHosname();
        }
        return null;
    }

    // 根据医院名称模糊查询医院信息
    @Override
    public List<Hospital> findByHosName(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    // 根据医院编号获取医院预约挂号详情
    @Override
    public Map<String, Object> findDetailHosp(String hoscode) {
        Map<String,Object> result = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        // 医院基本信息 包含等级
        result.put("hospital",hospital);
        // 单独处理更直观
        result.put("bookingRule",hospital.getBookingRule());
        // 不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    // 获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        // 根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}
